<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@include file="../head.jsp"%>
<html>
<head>
    <script type="text/javascript">
        (function(){
            Ext.onReady(function(){
                Ext.QuickTips.init();
                var id = '';

                Ext.define('Group', {
                    extend: 'Ext.data.Model',
                    fields: [
                        {name:'id',type:'string'},
                        {name:'name',type:'string'},
                        {name:'groupDesc',type:'string'},
                        {name:'status',type:'int'},
                        {name:'version',type:'int'}
                    ]
                });

                Ext.define('Operation', {
                    extend: 'Ext.data.Model',
                    fields: [
                        {name: 'id',type: 'string'},
                        {name: 'parentId',type: 'string'},
                        {name: 'menuName',type: 'string'},
                        {name: 'menuLink',type: 'string'},
                        {name: 'checked',type: 'boolean'},
                        {name: 'text',type: 'string'}
                    ]
                });

                var queryName = Ext.create('Ext.form.field.Text',{
                    id:'queryName',
                    name:'queryName'
                });

                var groupGrid = Ext.create('Ext.grid.Panel',{
                    title: '用户组列表',
                    loadMask: true,
                    autoScroll:true,
                    columns:[
                        Ext.create('Ext.grid.RowNumberer'),
                        {text: "用户组名",dataIndex: 'name',sortable: true},
                        {text: "用户组描述",width:250,dataIndex: 'groupDesc',sortable: true}
                    ],
                    tbar:Ext.create('Ext.toolbar.Toolbar',{
                        items:['-',{
                            xtype:'label',
                            text:'用户组名:'
                        },queryName,'-',{
                            xtype:'button',
                            iconCls:'find',
                            text:'查询',
                            handler:function(){
                                groupGrid.store.load();
                            }
                        },'-']
                    }),
                    store:Ext.create('Ext.data.Store',{
                        model: 'Group',
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sys/group!getGroup.action',
                            reader: {
                                root: 'root',
                                totalProperty: 'totalProperty'
                            }
                        },
                        autoLoad: true,
                        listeners:{
                            beforeload:function(){
                                groupGrid.store.proxy.extraParams = {
                                    name:encodeURIComponent(queryName.getValue()),
                                    status:1
                                }
                            }
                        }
                    }),
                    listeners:{
                        itemclick:function(view,record,item,index,event,options){
                            id = record.data.id;
                            tree.store.load();
                        }
                    }
                });


                var tree = Ext.create('Ext.tree.Panel', {
                    title:'系统菜单',
                    autoScroll: true,
                    split: true,
                    rootVisible: false,
                    lines:true,
                    height:"100%",
                    autoWidth:true,
                    containerScroll: true,
                    useArrows: true,
                    tbar:Ext.create('Ext.toolbar.Toolbar',{
                        items:['-',{
                            xtype:'button',
                            iconCls:'accept',
                            text:'保存',
                            handler:function(){
                                if(SysBase.isBlank(id)){
                                    Ext.Msg.show({title:'提示',msg:'请选择对应用户组',icon:Ext.Msg.WARNING,buttons:Ext.Msg.OK});
                                    return;
                                }
                                var check = tree.getChecked();
                                var ids = '';
                                for(var i = 0 , l = check.length ; i < l ; i ++ ){
                                    ids = ids + check[i].data.id + ',';
                                }
                                var params = {};
                                params.id = id;
                                params.ids = ids;
                                var data = {};
                                data.url = '/fsxt/sys/groupOperation!saveGroupOperation.action';
                                data.params = params;
                                callAjax.call(data);
                            }
                        },'-']
                    }),
                    store:Ext.create('Ext.data.TreeStore', {
                        model:'Operation',
                        autoLoad:true,
                        proxy: {
                            type:'ajax',
                            url:'/fsxt/sys/groupOperation!getOperation.action'
                        },
                        root: {
                            id: '',
                            text: '系统菜单',
                            expanded: true
                        },
                        listeners:{
                            load:function(){
                                tree.expandAll();
                            },
                            beforeload:function(){
                                this.proxy.extraParams = {
                                    id:id
                                }
                            }
                        }
                    }),
                    listeners:{
                        checkchange:function(node,checked){
                            if(checked && SysBase.isNotBlank(node.data.parentId)){
                                var parent = tree.store.getNodeById(node.data.parentId);
                                parent.set('checked', true);
                                tree.fireEvent('checkchange',parent,true);
                            }
                        }
                    }
                });


                new Ext.Viewport({
                    border:false,
                    layout:'border',
                    items:[{
                        width:450,
                        region:'west',
                        split:true,
                        items: [groupGrid]
                    },{
                        region:'center',
                        items: [tree]
                    }]
                });

            });
        })();
    </script>
</head>
<body>
</body>
</html>